import java.io.*;
import java.net.*;

/**
 * Creat with IntelliJ IDEA
 * Description:
 * User:WZW
 * Date:2022-03-28
 * Time:11:18
 */
public class Demo1 {
    public static void main(String[] args) throws IOException {
        URL url = new URL("https://www.szu.edu.cn");
        HttpURLConnection connection = (HttpURLConnection)url.openConnection();
        InputStream inputStream = connection.getInputStream();
        FileOutputStream fileOutputStream = new FileOutputStream(new File("szu.html"));
        int a = 0;
        while (a>-1){
            a = inputStream.read();
            fileOutputStream.write(a);
        }
        System.out.println("文件下载成功！");
        File file = new File("szu.html");
        System.out.println("文件大小为："+file.length());
    }



    public static void main1(String[] args) throws UnknownHostException {
//        InetAddress inetAddress = InetAddress.getLocalHost();
//        System.out.println(inetAddress.getHostName());//获取本机名
//        System.out.println(inetAddress.getHostAddress());//获取IP地址

        //根据要查询的网站实例化InetAddress类
        InetAddress[] inetAddresses = InetAddress.getAllByName("www.csdn.net");
        System.out.println(inetAddresses.length);//输出获取到的IP地址个数
        for (InetAddress i:inetAddresses) {
            //遍历每一个InetAddress对象
            byte[] addr = i.getAddress();//获取原始IP地址
            //将byte数据拼接成IP地址
            StringBuilder address = new StringBuilder();
            for (int j = 0; j < addr.length; j++) {
                address.append(addr[j]<0?addr[j]+256:addr[j]+0);//将负数转化为正数
                if (j<addr.length-1){
                    address.append('.');
                }
            }
            System.out.println("原始IP地址："+address);
            System.out.println("主机名/IP地址："+i.toString());
            System.out.println("散列码："+i.hashCode());
            System.out.println("是否多播地址："+i.isMulticastAddress());
        }
    }
}
